package com.mystack;



/**
 * @BelongsProject: DataStructures
 * @BelongsPackage: com.stack
 * @Author: 江岸
 * @CreateTime: 2020-12-11 22:44
 * @Description: 数组模拟栈
 */
public class ArrStackDemo {
    public static void main(String[] args) {
        ArrStack arrStack = new ArrStack(4);
        arrStack.push(2);
        arrStack.push(5);
        arrStack.push(7);
        arrStack.push(89);
        arrStack.show();
        arrStack.pop();
        //System.out.println("----------------------------------");
        //arrStack.show();
        ArrStackDemo arrStackDemo = new ArrStackDemo();
        System.out.println("lll"+arrStackDemo.hashCode());
    }
}
class ArrStack{
    private int maxSize;
    private int[] arr;
    private int top = -1;

    public ArrStack(int maxSize){
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    public boolean isFull(){
        if(top == maxSize-1){
            return true;
        }
        return false;
    }

    public  boolean isEmpty(){
        if (top == -1){
            return true;
        }
        return false;
    }

    public  void push(int num){
        if(isFull()){
            System.out.println("栈已满");
        }
        top++;
        arr[top] = num;
    }

    public  int pop(){
        if (isEmpty()){
            throw new RuntimeException("栈为空");
        }
        int num = arr[top];
        top--;
        return num;
    }

    public  void  show(){
        if (isEmpty()){
            throw new RuntimeException("栈为空");
        }
        //从后往前遍历
        for(int i=top;i>=0;i--){
            System.out.printf("arr[%d]=%s,",i,arr[i]);
        }
    }


}
